package com.ruoyi.project.applet.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.applet.domain.Order;
import com.ruoyi.project.applet.domain.Shipping;
import com.ruoyi.project.applet.domain.Wallelog;
import com.ruoyi.project.applet.service.IOrderService;
import com.ruoyi.project.applet.service.impl.WallelogServiceImpl;
import com.ruoyi.project.system.domain.SysUserWx;
import io.swagger.annotations.ApiOperation;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单Controller
 *
 * @author ruoyi
 * @date 2025-02-07
 */
@RestController
@RequestMapping("/applet/order")
@ApiOperation("订单接口")
public class OrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private WallelogServiceImpl wallelogService;

    /**
     * PC查询订单列表
     */
    @ApiOperation("PC查询订单列表")
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        List<Order> list = orderService.selectOrderList(order);
        return getDataTable(list);
    }


    /**
     * 用户查询分销订单
     */
    @ApiOperation("用户查询分销订单")
    @GetMapping("/list/wx")
    public TableDataInfo listBytoken() {
        LoginUser loginUser = getLoginUser();
        SysUserWx userWx = loginUser.getUserWx();
        Assert.notNull(userWx, "请先登录");
        Wallelog wallelog = new Wallelog();
        wallelog.setUserId(userWx.getOpenId());
        wallelog.setType("0");
        List<Wallelog> wallelogList = wallelogService.selectWallelogList(wallelog);
        List<Order> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(wallelogList)) {
            List<String> orderNumbers = wallelogList.stream().map(Wallelog::getOrderNumber).collect(Collectors.toList());
            startPage();
            result = orderService.selectFenxiaoOrderList(orderNumbers);
        }
        return getDataTable(result);
    }


    /**
     * 获取订单详细信息
     */
    @ApiOperation("查看订单详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(orderService.selectOrderById(id));
    }


    /**
     * 修改订单状态
     */
    @ApiOperation("修改订单状态")
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateOrder(order));
    }


}
